from backend.app import db
from datetime import datetime

class LotteryResult(db.Model):
    """双色球开奖数据表"""
    __tablename__ = 'lottery_results'
    
    id = db.Column(db.Integer, primary_key=True)
    draw_date = db.Column(db.Date, nullable=False, comment='开奖日期')
    draw_number = db.Column(db.String(20), unique=True, nullable=False, comment='期数')
    red_ball_1 = db.Column(db.Integer, nullable=False, comment='红球1')
    red_ball_2 = db.Column(db.Integer, nullable=False, comment='红球2')
    red_ball_3 = db.Column(db.Integer, nullable=False, comment='红球3')
    red_ball_4 = db.Column(db.Integer, nullable=False, comment='红球4')
    red_ball_5 = db.Column(db.Integer, nullable=False, comment='红球5')
    red_ball_6 = db.Column(db.Integer, nullable=False, comment='红球6')
    blue_ball = db.Column(db.Integer, nullable=False, comment='蓝球')
    prize_info = db.Column(db.Text, comment='中奖信息')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def get_red_balls(self):
        """获取红球列表"""
        return [self.red_ball_1, self.red_ball_2, self.red_ball_3, 
                self.red_ball_4, self.red_ball_5, self.red_ball_6]
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'draw_date': self.draw_date.isoformat() if self.draw_date else None,
            'draw_number': self.draw_number,
            'red_ball_1': self.red_ball_1,
            'red_ball_2': self.red_ball_2,
            'red_ball_3': self.red_ball_3,
            'red_ball_4': self.red_ball_4,
            'red_ball_5': self.red_ball_5,
            'red_ball_6': self.red_ball_6,
            'blue_ball': self.blue_ball,
            'prize_info': self.prize_info,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat()
        }
    
    def __repr__(self):
        return f'<LotteryResult {self.draw_number}>' 